from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

from stw_app import models
from stw_app.consumers import system_notify
from stw_app.models import WantToWorker
from stw_app.decorator import already_login
from datetime import datetime

# Create your views here.
@already_login
@csrf_exempt
def complete_order(request):
    if request.method == "POST":
        order_id = request.POST.get('order_id')
        user_id = int(request.POST.get('user_id'))
        order = models.Order.objects.get(id=order_id)
        if user_id == order.customer_id:
            order.condition = 3
            # 完成订单提醒
            try:
                # 向目标用户发送系统通知
                order_obj = models.Order.objects.get(id=order_id)  # 订单对象
                user_obj_sender = models.UserInfo.objects.get(id=request.session['userid'])  # 当前用户对象
                user_obj_receiver = models.UserInfo.objects.get(id=order.real_worker_id)  # 接单的对象
                system_notify(user_obj_receiver.id,
                              f"用户 {user_obj_sender.name} 已将订单 {order_obj.title} 完成！")
            except Exception as e:
                print(e)

        elif user_id == order.real_worker_id:
            order.condition = 2

            try:
                # 向目标用户发送系统通知
                order_obj = models.Order.objects.get(id=order_id)  # 订单对象
                user_obj_sender = models.UserInfo.objects.get(id=request.session['userid'])  # 当前用户对象
                user_obj_receiver = models.UserInfo.objects.get(id=order.customer_id)  # 接单的对象
                system_notify(user_obj_receiver.id,
                              f"用户 {user_obj_sender.name} 已将订单 {order_obj.title} 完成！")
            except Exception as e:
                print(e)
        order.save()

        return JsonResponse({'success': True})
    return JsonResponse({'success': False})

@already_login
@csrf_exempt
def cancel_order(request):
    if request.method == "POST":
        order_id = request.POST.get('order_id')  # get中填入订单的id对应的前端传来的值
        order = models.Order.objects.get(id=order_id)  # 把Order换成对应的数据库类名称
        order.condition = 4
        order.save()

        try:
            # 向目标用户发送系统通知
            order_obj = models.Order.objects.get(id=order_id)  # 订单对象
            user_obj_sender = models.UserInfo.objects.get(id=request.session['userid'])  # 当前用户对象
            user_obj_receiver = models.UserInfo.objects.get(id=order_obj.customer_id)  # 发单的对象
            system_notify(user_obj_receiver.id,
                          f"用户 {user_obj_sender.name} 向您的订单 {order_obj.title} 发送取消接单通知！")
        except Exception as e:
            print(e)

        return JsonResponse({'success': True})
    return JsonResponse({'success': False})

@already_login
@csrf_exempt
def delete_order(request):
    if request.method == "POST":
        order_id = request.POST.get('order_id')  # get中填入订单的id对应的前端传来的值
        order = models.Order.objects.get(id=order_id)  # 把Order换成对应的数据库类名称
        order.delete()
        return JsonResponse({'success': True})
    return JsonResponse({'success': False})

@already_login
@csrf_exempt
def progressing_orders_dict(request):
    if request.method == "POST":
        dict_list = []
        real_worker_id = request.POST.get('user_id')  # get中填入订单的id对应的前端传来的值
        recieved_orders = models.Order.objects.filter(real_worker_id=real_worker_id)  # 把Order换成对应的数据库类名称
        for order_element in recieved_orders:
            if order_element.condition < 3:
                dict = {
                    'id': order_element.id,
                    'customer_id':order_element.customer_id,
                    'real_worker_id': order_element.real_worker_id,
                    'condition': order_element.condition,
                    'description': order_element.description,
                    'title': order_element.title,
                    'price': order_element.price,
                    'type': order_element.type,
                    'start_time': order_element.start_time,
                    'end_time': order_element.end_time,
                    'release_time': order_element.release_time,
                }
                dict_list.append(dict)
        customer_id = request.POST.get("user_id")  # get中填入订单的id对应的前端传来的值
        published_orders = models.Order.objects.filter(customer_id=customer_id)  # 把Order换成对应的数据库类名称
        for order_element in published_orders:
            if order_element.condition < 3:
                dict = {
                    'id': order_element.id,
                    'customer_id': order_element.customer_id,
                    'real_worker_id': order_element.real_worker_id,
                    'condition': order_element.condition,
                    'description': order_element.description,
                    'title': order_element.title,
                    'price': order_element.price,
                    'type': order_element.type,
                    'start_time': order_element.start_time,
                    'end_time': order_element.end_time,
                    'release_time': order_element.release_time,
                }
                dict_list.append(dict)
        return JsonResponse({'data': dict_list, 'success': True})
    return JsonResponse({'success': False})

@already_login
@csrf_exempt
def published_orders_dict(request):
    if request.method == "POST":
        dict_list = []
        customer_id = request.POST.get("user_id")  # get中填入订单的id对应的前端传来的值
        orders = models.Order.objects.filter(customer_id=customer_id)  # 把Order换成对应的数据库类名称
        for order_element in orders:
            dict = {
                'id': order_element.id,
                'customer_id': order_element.customer_id,
                'real_worker_id': order_element.real_worker_id,
                'condition': order_element.condition,
                'description': order_element.description,
                'title': order_element.title,
                'price': order_element.price,
                'type': order_element.type,
                'start_time': order_element.start_time,
                'end_time': order_element.end_time,
                'release_time': order_element.release_time,
            }
            dict_list.append(dict)
        return JsonResponse({'data': dict_list, 'success': True})
    return JsonResponse({'success': False})

@already_login
@csrf_exempt
def recieved_orders_dict(request):
    if request.method == "POST":
        dict_list = []
        real_worker_id = request.POST.get('user_id')  # get中填入订单的id对应的前端传来的值
        orders = models.Order.objects.filter(real_worker_id=real_worker_id)  # 把Order换成对应的数据库类名称
        for order_element in orders:
            dict = {
                'id': order_element.id,
                'customer_id': order_element.customer_id,
                'real_worker_id': order_element.real_worker_id,
                'condition': order_element.condition,
                'description': order_element.description,
                'title': order_element.title,
                'price': order_element.price,
                'type': order_element.type,
                'start_time': order_element.start_time,
                'end_time': order_element.end_time,
                'release_time': order_element.release_time,
            }
            dict_list.append(dict)
        return JsonResponse({'data': dict_list, 'success': True})
    return JsonResponse({'success': False})

@already_login
@csrf_exempt
def all_orders_dict(request):
    if request.method == "POST":
        dict_list = []
        real_worker_id = request.POST.get('user_id')  # get中填入订单的id对应的前端传来的值
        recieved_orders = models.Order.objects.filter(real_worker_id=real_worker_id)  # 把Order换成对应的数据库类名称
        for order_element in recieved_orders:
            if order_element.condition > 3:
                dict = {
                    'id': order_element.id,
                    'customer_id': order_element.customer_id,
                    'real_worker_id': order_element.real_worker_id,
                    'condition': order_element.condition,
                    'description': order_element.description,
                    'title': order_element.title,
                    'price': order_element.price,
                    'type': order_element.type,
                    'start_time': order_element.start_time,
                    'end_time': order_element.end_time,
                    'release_time': order_element.release_time,
                }
                dict_list.append(dict)
        customer_id = request.POST.get("user_id")  # get中填入订单的id对应的前端传来的值
        published_orders = models.Order.objects.filter(customer_id=customer_id)  # 把Order换成对应的数据库类名称
        for order_element in published_orders:
            if order_element.condition > 3:
                dict = {
                    'id': order_element.id,
                    'customer_id': order_element.customer_id,
                    'real_worker_id': order_element.real_worker_id,
                    'condition': order_element.condition,
                    'description': order_element.description,
                    'title': order_element.title,
                    'price': order_element.price,
                    'type': order_element.type,
                    'start_time': order_element.start_time,
                    'end_time': order_element.end_time,
                    'release_time': order_element.release_time,
                }
                dict_list.append(dict)
        return JsonResponse({'data': dict_list, 'success': True})
    return JsonResponse({'success': False})


@csrf_exempt
def max(request):
    if request.method == "POST":
        num = models.Order.objects.order_by('-id').first().id
        return JsonResponse({'data': num})


#
# @csrf_exempt
# def store_in_wtw(request):  # 1
#     if request.method == "POST":
#         order_id = request.POST.get("order_id")  # get中填入订单的id对应的前端传来的值
#         worker_id = request.POST.get("worker_id")  # get中填入订单的接单人对应的前端传来的值
#         want_to_work = WantToWorker()  # 把Employ换成对应的数据库类名称
#         want_to_work.id = order_id
#         want_to_work.want_to_worker_id = worker_id
#         want_to_work.save()
#         return JsonResponse({'success': True})
#     return JsonResponse({'success': False})
#
#
#

#
# @csrf_exempt
# def judgement_store_in(request):  # 2
#     if request.method == "POST":
#         order_id = request.POST.get('order_id')  # get中填入订单的id对应的前端传来的值
#         releaser_id = request.POST.get('releaser_id')  # get中填入订单的接单人对应的前端传来的值
#         judged_id = request.POST.get('judged_id')  # get中填入订单的接单人对应的前端传来的值
#         judgement_info = request.POST.get('judgement_info')  # get中填入订单的接单人对应的前端传来的值
#         score = request.POST.get('score')  # get中填入订单的接单人对应的前端传来的值
#         judgement = Judgement()  # 把EmployJudgement换成对应的数据库类名称
#         judgement.order_id = order_id
#         judgement.releaser_id = releaser_id
#         judgement.judged_id = judged_id
#         judgement.judgment_info = judgement_info
#         judgement.score = score
#         return JsonResponse({'success': True})
#     return JsonResponse({'success': False})
#
#
# @csrf_exempt
# def confirm_order(request):  # 4
#     if request.method == "POST":
#         order_id = request.POST.get('order_id')  # get中填入订单的id对应的前端传来的值
#         real_worker_id = request.POST.get('real_worker_id')  # get中填入订单的接单人对应的前端传来的值
#         orders = models.Order.objects.filter(id=id)  # 把Order换成对应的数据库类名称
#         for order_element in orders:
#             order_element.condition = 1
#             order_element.real_worker_id = real_worker_id
#         return JsonResponse({'success': True})
#     return JsonResponse({'success': False})
#

def comment(request):
    if request.method == "POST":
        value1 = request.POST.get('value1')
        value2 = request.POST.get('value2')
        user_id = request.POST.get('user_id')
        comment = request.POST.get('comment')
        order_id = request.POST.get('order_id')
        order = models.Order.objects.get(id = order_id)
        judged_id = order.real_worker_id
        new = models.Judgement()
        new.belief_score = value1
        new.score = value2
        new.releaser_id = user_id
        new.judgement_info = comment
        new.order_id = order_id
        new.judged_id = judged_id
        current_datetime = datetime.now()
        # print("当前时间：", current_datetime)
        new.release_time = current_datetime
        # 保存到数据库中
        new.save()
        return JsonResponse({'success': True})
    return JsonResponse({'success': False})